<template>
  <!-- 增改用户弹框 -->
  <hcDialog
    dialogWidth="30%"
    :dialogTitle="options.type === 'add' ? '新增库存' : '编辑库存'"
    :isVisable="options.show"
    @closeDialog="closeFun"
    @openDialog="openFun"
  >
    <el-form
      ref="formRef"
      :model="form"
      label-width="6vw"
      status-icon
      :rules="rules"
      :validate-on-rule-change="false"
    >
      <el-form-item prop="inventory_id" label="仓库名称">
        <el-select v-model="form.inventory_id">
          <el-option
            v-for="item of inventArr"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="material_id" label="物料名称">
        <el-select v-model="form.material_id">
          <el-option
            v-for="item of materialArr"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="onhand_quantity" label="库存数量">
        <el-input v-model="form.onhand_quantity"> </el-input>
      </el-form-item>

      <el-form-item>
        <div class="btns">
          <el-button type="primary" @click="saveDialog()">{{
            t("common.saveText")
          }}</el-button>
          <el-button @click="closeFun">{{ t("common.cancelText") }}</el-button>
        </div>
      </el-form-item>
    </el-form>
  </hcDialog>
</template>
<script setup>
import { onMounted, reactive, ref } from "vue";

import {
  AddOnhand,
  updateOnhand,
  getInventory,
  getMaterial,
} from "@/api/material";
import { useI18n } from "vue-i18n";
let { t } = useI18n();

const emit = defineEmits(["closeDialog"]);

const props = defineProps({
  options: {
    type: Object,
    default: () => {
      return {
        show: false,
        type: "add", // 用于判断是编辑还是删除 add edit
        data: null, // 当前编辑的用户
      };
    },
  },
});

const formRef = ref();
const form = reactive({
  id: 0,
  material_id: "",
  inventory_id: "",
  inventory_code: "",
  onhand_quantity: 0,
  enabled: true,
});
let materialArr = ref([]);
let inventArr = ref([]);

const rules = {
  // inventory_code: [
  //   { required: true, message: "请输入库存编号", trigger: "blur" },
  // ],
  // inventory_name: [
  //   { required: true, message: "请输入库存名称", trigger: "blur" },
  // ],
};

function saveDialog() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      let res = "";
      if (props.options.type === "edit") {
        res = await updateOnhand(form);
      } else {
        res = await AddOnhand(form);
      }
      if (res) {
        ElMessage.success("保存成功");
        emit("closeDialog", true);
      } else {
        ElMessage.error("保存失败");
      }
    }
  });
}

// 关闭弹框
const closeFun = () => {
  formRef.value?.resetFields();
  emit("closeDialog");
};

function openFun() {
  if (props.options.type === "edit") {
    for (let key in form) {
      form[key] = props.options.data[key];
    }
  }
}

async function getAllInvent() {
  let res = await getInventory();
  inventArr.value = res?.items
    ?.filter((item) => item.enabled)
    ?.map((item) => {
      return {
        value: item.id,
        label: item.inventory_name,
      };
    });
}
async function getAllMaterial() {
  let res = await getMaterial();
  materialArr.value = res?.items
    ?.filter((item) => item.enabled)
    ?.map((item) => {
      return {
        value: item.id,
        label: item.material_name,
      };
    });
}

onMounted(() => {
  getAllMaterial();
  getAllInvent();
});
</script>
<style lang="scss" scoped></style>
